
//-------------------------------------json数组对象，加载列表--------------------------------------------
    var obj = [{
    "id": "01",
    "title": "鲜花/柔情蜜意-11枝戴安娜粉玫瑰，白百合2枝",
    "oldPrice": "￥390",
    "pirce": "￥230",
    "img": "img/1.jpg",
    "types": "鲜花 编 号：9012009",
    "material": "33枝红玫瑰，石竹梅围绕",
    "pack": "黑色条纹纸，红色缎带花结",
    "flowerLanguage": "只想和你忘掉一切烦恼，尽情沉醉在最浪漫的气氛中。"
}, {
    "id": "02",
    "title": "【泰安馆】山东富世康麦芯挂面面条手擀鸡蛋挂面 多规格 细中粗可选 500g*3包 ",
    "oldPrice": "￥410",
    "pirce": "￥360",
    "img": "img/2.jpg",
    "types": "鲜花 编 号：9012009",
    "material": "33枝红玫瑰，石竹梅围绕",
    "pack": "黑色条纹纸，红色缎带花结",
    "flowerLanguage": "只想和你忘掉一切烦恼，尽情沉醉在最浪漫的气氛中。"
}, {
    "id": "03",
    "title": "伊都拉面棍棍面500g*5包鲜拉面湿面半干面拌面炒面油泼面水煮速食拉面挂面 ",
    "oldPrice": "￥210",
    "pirce": "￥110",
    "img": "img/3.jpg",
    "types": "鲜花 编 号：9012009",
    "material": "33枝红玫瑰，石竹梅围绕",
    "pack": "黑色条纹纸，红色缎带花结",
    "flowerLanguage": "只想和你忘掉一切烦恼，尽情沉醉在最浪漫的气氛中。"
}, {
    "id": "04",
    "title": "仙力面业 2kg刀削面宽面油泼面凉面农家手工筋道整箱挂面面条包邮4斤",
    "oldPrice": "￥740",
    "pirce": "￥666",
    "img": "img/4.jpg",
    "types": "鲜花 编 号：9012009",
    "material": "33枝红玫瑰，石竹梅围绕",
    "pack": "黑色条纹纸，红色缎带花结",
    "flowerLanguage": "只想和你忘掉一切烦恼，尽情沉醉在最浪漫的气氛中。"
}, {
    "id": "05",
    "title": "贝因美（Beingmate） 全能优+面条 果蔬营养面条260克果蔬营养面条260克",
    "oldPrice": "￥598",
    "pirce": "￥530",
    "img": "img/5.jpg",
    "types": "鲜花 编 号：9012009",
    "material": "33枝红玫瑰，石竹梅围绕",
    "pack": "黑色条纹纸，红色缎带花结",
    "flowerLanguage": "只想和你忘掉一切烦恼，尽情沉醉在最浪漫的气氛中。"
}, {
    "id": "06",
    "title": "重庆特产美食小吃正宗麻辣小面重庆小面方便面175gx5速食调料面条DF",
    "oldPrice": "￥120",
    "pirce": "￥99",
    "img": "img/6.jpg",
    "types": "鲜花 编 号：9012009",
    "material": "33枝红玫瑰，石竹梅围绕",
    "pack": "黑色条纹纸，红色缎带花结",
    "flowerLanguage": "只想和你忘掉一切烦恼，尽情沉醉在最浪漫的气氛中。"
}, {
    "id": "07",
    "title": "贝因美（Beingmate） 全能优+面条 果蔬营养面条260克果蔬营养面条260克",
    "oldPrice": "￥616",
    "pirce": "￥530",
    "img": "img/7.jpg",
    "types": "鲜花 编 号：9012009",
    "material": "33枝红玫瑰，石竹梅围绕",
    "pack": "黑色条纹纸，红色缎带花结",
    "flowerLanguage": "只想和你忘掉一切烦恼，尽情沉醉在最浪漫的气氛中。"
}, {
    "id": "08",
    "title": "重庆特产美食小吃正宗麻辣小面重庆小面方便面175gx5速食调料面条DF",
    "oldPrice": "￥128",
    "pirce": "￥99",
    "img": "img/8.jpg",
    "types": "鲜花 编 号：9012009",
    "material": "33枝红玫瑰，石竹梅围绕",
    "pack": "黑色条纹纸，红色缎带花结",
    "flowerLanguage": "只想和你忘掉一切烦恼，尽情沉醉在最浪漫的气氛中。"
}];

    var oList = document.getElementById("list");
    //判断一下当前cookie是否存在 如果存在的话获取出cookie的值

    if(getCookie("init_")){
        //将cookie的字符串对象转换为对象
        var data = JSON.parse(getCookie("init_"));
        var str = "";

        //将data与obj进行比较 如果id是一样的话就找出相对应的商品了 那么就可以进行字符串拼然后放在页面上即可

        for(var i in data){         
            for(var k in obj){

                if(i == obj[k].id){                 
                    str+=`<li data-id="${obj[k].id}">
                <input type="checkbox" class="item">
                <div class="p-goods">
                    <div class="p-img">
                        <a href=""><img alt="" src="${obj[k].img}"></a>
                    </div>
                    <div class="item-msg">
                        <div class="p-name">
                                <a href="" class="p-title"></a>

                        </div>
                        <div class="p-extend p-extend-new">
                            <span class="promise return-y">
                                <i class="return-y-icon"></i>
                                <a href="#none" class="ftx-08">支持7天无理由退货</a>
                            </span>
                            <span class="promise">
                                <i class="jd-service-icon"></i>
                                <a data-tips="选延保" class="ftx-03 jd-service" href="">选延保</a>
                            </span>
                        </div>
                    </div>
                </div>

                <div class="cell p-props p-props-new">
                    <div class="props-txt">${obj[k].oldPrice}</div>
                </div>
                
                <div class="p-price">
                    <span class="price">${obj[k].pirce}</span>
                    <span class="chu">促销∨</span>
                    <span class="free">白条三期免息</span>
                </div>

                <div class="p-quantity">
                        <div class="quantity-form">
                            <input type="button" value="-" class="sub">
                            <input type="text" value="${data[i]}" class="itxt">
                            <input type="button" value="+" class="add">
                        </div>
                        <div class="quantity-txt">有货</div>
                </div>
                <div class="p-sum">
                    <span class="sum-price">${toDecimal2(data[i]*obj[k].pirce.substr(1))}</span>
                </div>
                <div class="p-deleat">
                    <a href="##" class="cart-remove">删除</a>
                    <a href="">移到我单位关注</a>
                    <a href="">加到我的关注</a>
                </div>
            </li>`;


             }


            }

        }
        oList.innerHTML = str;


// ------------------------全选框效果代码结束------------------------------

    var aSub = document.querySelectorAll(".sub");//获取减号按钮
    var aAdd = document.querySelectorAll(".add");//获取加号按钮
    var aPrice = document.querySelectorAll(".price");//获取单价
    var oList = document.getElementById("list");

    // 事件代理方式：
   
/*    oList.onclick = function(e) {
            var e = e||event;
            var target = e.target ||e.srcElement;
             // 点击减号按钮操作
            if(target.tagName == "INPUT" && target.className == "sub"){

                // data[i]-=1;
                console.log(data);//这里让每次点击时cookie里的数量减1

                var numS= Number(target.nextElementSibling.value);
                    if(numS==1){
                        numS=1;
                    }else{
                        numS--;
                    }           
                    target.nextElementSibling.value = numS;
                    // 获取单价
                    var li=target.parentNode.parentNode.parentNode;
                    var priceS =Number(obj[k].pirce.substr(1));
                    var floatSum=toDecimal2(priceS*numS);
                    li.children[5].firstElementChild.innerHTML ="￥"+floatSum;
            }

            // 点击加号按钮操作
            if(target.tagName == "INPUT" && target.className == "add"){
               
              //获取到当前商品的id
                var numIda = target.parentNode.parentNode.parentNode.getAttribute("data-id");
                //删除掉它里面的那个id作为key的data值value
                 data[numIda]+=1;
                
                //将对象转换为字符串然后再重新存入cookie
                var dataStrs = JSON.stringify(data);
                setCookie("init_",dataStrs,10);

                console.log(data);//这里让每次点击时cookie里的数量加1



                var num = Number(target.previousElementSibling.value);
                num++;
                target.previousElementSibling.value = num;
                // 获取单价

                var li=target.parentNode.parentNode.parentNode;
                var price =Number(obj[k].pirce.substr(1));
                var floatSum=toDecimal2(price*num);
                li.children[5].firstElementChild.innerHTML ="￥"+floatSum;
            }

            //删除商品
            if(target.tagName == "A" && target.className == "cart-remove"){
                  target.parentNode.parentNode.remove();


                     //获取到当前商品的id
                    var numId = target.parentNode.parentNode.getAttribute("data-id");
                    //删除掉它里面的那个id作为key的data值value
                    delete data[numId];
                    
                    //将对象转换为字符串然后再重新存入cookie
                    var dataStrs = JSON.stringify(data);
                    setCookie("init_",dataStrs,10);

            }   




    }*/


 // console.log(i , + data[i]);  



      





        


















    }


 //------------------------------------计算出添加的商品数量-----------------------------------
 // setInterval(function(){
 //     var aLi=document.querySelectorAll(".input-list>li");
 //    var countP="";
 //    countP=aLi.length;
 //    document.querySelector(".produSum").innerHTML=countP;
 // },500) 


// ----------------------------计算总价---------------------------------------------------
//制保留2位小数，如：2，会在2后面补上00.即2.00    
    function toDecimal2(x) {    
        var f = parseFloat(x);    
        if (isNaN(f)) {    
            return false;    
        }    
        var f = Math.round(x*100)/100;    
        var s = f.toString();    
        var rs = s.indexOf('.');    
        if (rs < 0) {    
            rs = s.length;    
            s += '.';    
        }    
        while (s.length <= rs + 2) {    
            s += '0';    
        }    
        return s;    
    }    

    // -----------------------------------------------全选框效果代码------------------------------------
     var all=document.querySelector("#all");
     var items=document.querySelectorAll(".item");
    //全选框选中则其他选项选中，再点击则其他也不选中
    all.onclick=function(){
        if(this.checked){
            for(var i=0;i<items.length;i++){
                items[i].checked=true;

            }
        }else{
            for(var i=0;i<items.length;i++){
                items[i].checked=false;
            }
        }

   }

    for(var i=0;i<items.length;i++){
        items[i].onclick=function(){
            var flag=true;//标签默认选中
            for(var j=0;j<items.length;j++){
                if(!items[j].checked){//只要存在有未被选中的框则全选框就不能被选中
                    flag=false;
                    break;
                }
            }
            if(flag){
                all.checked=true;
            }else{
                all.checked=false;
            }
        }
    }